package bookManageSystem.dao;

import bookManageSystem.po.Employee;
import bookManageSystem.po.Order;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OrderDAO {

    private static final String USER = "root";
    private static final String PASS = "123456";
    private static final String DB_URL = "jdbc:mysql://localhost:3306/huaide?useUnicode=true&characterEncoding=UTF-8";

    private Connection getConnection() throws SQLException {
        return DriverManager.getConnection(DB_URL, USER, PASS);
    }

    public boolean addOrder(Order order) {
        String sql = "INSERT INTO orders (name, docke, time, cost) VALUES (?, ?, ?, ?)";
        try (Connection conn = getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, order.getName());
            stmt.setString(2, order.getDocke());
            stmt.setString(3, order.getTime());
            stmt.setDouble(4, order.getCost());

            int rowsAffected = stmt.executeUpdate();
            return rowsAffected > 0; // 如果影响的行数大于0，说明插入成功
        } catch (SQLException e) {
            e.printStackTrace();
            return false; // 如果发生异常，返回false
        }
    }


    public List<Order> selectOrder() {
        List<Order> orderList =  new ArrayList<>();
        String sql = "SELECT * FROM orders";
        try (Connection conn = getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                Order order = new Order();
                order.setId(rs.getInt("id"));
                order.setName(rs.getString("name"));
                order.setDocke(rs.getString("docke"));
                order.setTime(rs.getString("time"));
                order.setCost(rs.getDouble("cost"));
                orderList.add(order);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return orderList;
    }
}
